import axios from 'axios'
import qs from 'qs'
export default {
  fileUpload (file, response) {
    var ossData = {
      accessid: '',
      policy: '',
      signature: '',
      dir: '',
      host: '',
      expire: '',
      callback: ''
    }
    var data = response.data
    if (data) {
      ossData = qs.parse(data.result)
    }
    var requestData = new FormData()
    var name = file.name
    // eslint-disable-next-line no-undef
    var fileName = getFileName(name)
    var key = ossData.dir + '/' + fileName
    // key就代表文件层级和阿里云上的文件名
    requestData.append('key', key)
    requestData.append('policy', ossData.policy)
    requestData.append('OSSAccessKeyId', ossData.accessid)
    // 阿里云正常返回204，然后返回信息是空的，想看到返回信息强制让它返回201，返回一个xml文件可以提取有用的信息
    requestData.append('success_action_status', 200)
    requestData.append('signature', ossData.signature)
    requestData.append('file', file, name)
    requestData.append('name', name)
    return requestData
  },
  getFileName (name) {
    getFileName(name)
  },
  randomWord (randomFlag, min, max) {
    randomWord(randomFlag, min, max)
  },
  getSignature (type) {
    return axios.post('/api/upload/getSignature', qs.stringify({type: type}))
  },
  getUUID () {
    return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
      (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16).toUpperCase()
    )
  },
  // get brower
  GetCurrentBrowser () {
    let ua = navigator.userAgent.toLocaleLowerCase()
    let browserType = null
    if (ua.match(/msie/) != null || ua.match(/trident/) != null) {
      browserType = 'IE'
    } else if (ua.match(/firefox/) != null) {
      browserType = 'firefox'
    } else if (ua.match(/ucbrowser/) != null) {
      browserType = 'UC'
    } else if (ua.match(/opera/) != null || ua.match(/opr/) != null) {
      browserType = 'opera'
    } else if (ua.match(/bidubrowser/) != null) {
      browserType = 'baidu'
    } else if (ua.match(/metasr/) != null) {
      browserType = 'sougou'
    } else if (ua.match(/tencenttraveler/) != null || ua.match(/qqbrowse/) != null) {
      browserType = 'QQ'
    } else if (ua.match(/maxthon/) != null) {
      browserType = 'maxthon'
    } else if (ua.match(/chrome/) != null) {
      var is360 = _mime('type', 'application/vnd.chromium.remoting-viewer')
      if (is360) {
        browserType = '360'
      } else {
        browserType = 'chrome'
      }
    } else if (ua.match(/safari/) != null) {
      browserType = 'Safari'
    } else {
      browserType = 'others'
    }
    return browserType
  },
  GetOs () {
    let sUserAgent = navigator.userAgent.toLocaleLowerCase()
    let isWin = (navigator.platform.toLowerCase() === 'win32') || (navigator.platform.toLowerCase() === 'windows')
    let isMac = (navigator.platform.toLowerCase() === 'mac68k') || (navigator.platform.toLowerCase() === 'macppc') || (navigator.platform.toLowerCase() === 'macintosh') || (navigator.platform.toLowerCase() === 'macintel')
    if (isMac) return 'Mac'
    var isUnix = (navigator.platform.toLowerCase() === 'x11') && !isWin && !isMac
    if (isUnix) return 'Unix'
    var isLinux = (String(navigator.platform).indexOf('linux') > -1)
    if (isLinux) return 'Linux'
    if (isWin) return 'Windows'

    if (sUserAgent.indexOf('android') > -1) return 'Android'
    if (sUserAgent.indexOf('iphone') > -1) return 'iPhone'
    if (sUserAgent.indexOf('symbianos') > -1) return 'SymbianOS'
    if (sUserAgent.indexOf('windows phone') > -1) return 'Windows Phone'
    if (sUserAgent.indexOf('ipad') > -1) return 'iPad'
    if (sUserAgent.indexOf('ipod') > -1) return 'iPod'
    return 'others'
  }
}
function getFileName (name) {
  var date = new Date()
  var startIndex = name.lastIndexOf('.')
  // eslint-disable-next-line no-unused-vars
  var houzhui = ''
  // eslint-disable-next-line eqeqeq
  if (startIndex != -1) {
    houzhui = name.substring(startIndex + 1, name.length).toLowerCase()
  }
  // eslint-disable-next-line eqeqeq
  if (houzhui.length == 0) {
    houzhui = name
  }
  // eslint-disable-next-line no-undef
  return String(date.getFullYear()) +
    String(date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1)) +
    String(date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) +
    String(date.getHours()) +
    String(date.getMinutes()) +
    String(date.getSeconds()) +
    randomWord(false, 6) + '.' + houzhui
}
function randomWord (randomFlag, min, max) {
  var str = ''
  var range = min
  var arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
  // 随机产生
  if (randomFlag) {
    range = Math.round(Math.random() * (max - min)) + min
  }
  for (var i = 0; i < range; i++) {
    let pos = Math.round(Math.random() * (arr.length - 1))
    str += arr[pos]
  }
  return str
}
function _mime (option, value) {
  var mimeTypes = navigator.mimeTypes
  for (var mt in mimeTypes) {
    if (mimeTypes[mt][option] === value) {
      return true
    }
  }
  return false
}
